﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace ZG_member.DAL
{
    public class taocanjiluDAL
    {
        public bool Exists(string memberId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from taocanjilu");
            strSql.Append(" where memberId=@memberId ");
            SqlParameter[] parameters = {
					new SqlParameter("@memberId", SqlDbType.NVarChar,50)
};
            parameters[0].Value = memberId;


            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList(string strWhere)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM taocanjilu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int Add(ZG_member.Model.taocanjilu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into taocanjilu(");
            strSql.Append("memberid,xiangqing)");
            strSql.Append(" values (");
            strSql.Append("@memberid,@xiangqing)");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.NVarChar,50),
					new SqlParameter("@xiangqing", SqlDbType.NVarChar,4000)};
            parameters[0].Value = model.Memberid;
            parameters[1].Value = model.Xiangqing;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int updatexiangqing(ZG_member.Model.taocanjilu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update taocanjilu set ");
            strSql.Append("xiangqing =@xiangqing ");
            strSql.Append(" where memberid = @memberid");
 
            SqlParameter[] parameters = {
					new SqlParameter("@xiangqing",SqlDbType.NVarChar,4000),
					new SqlParameter("@memberid",  SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Xiangqing;
            parameters[1].Value = model.Memberid;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int updatememberid(string newid, string oldid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update taocanjilu set ");
            strSql.Append("memberid =@oldid ");
            strSql.Append(" where memberid = @memberid");

            SqlParameter[] parameters = {
					new SqlParameter("@oldid",SqlDbType.NVarChar,4000),
					new SqlParameter("@memberid",  SqlDbType.NVarChar,50)};
            parameters[0].Value = oldid;
            parameters[1].Value = newid;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        public bool Delete(string memberid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from taocanjilu ");
            strSql.Append(" where memberid=@memberid");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.NVarChar,50)
};
            parameters[0].Value = memberid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public ZG_member.Model.taocanjilu GetModelFormemberCardId(string memberid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,memberid,xiangqing from taocanjilu ");
            strSql.Append(" where memberid=@memberid");
            SqlParameter[] parameters = {
					new SqlParameter("@memberId", SqlDbType.NVarChar,50)
};
            parameters[0].Value = memberid;

            ZG_member.Model.taocanjilu model = new ZG_member.Model.taocanjilu();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["memberid"] != null && ds.Tables[0].Rows[0]["memberid"].ToString() != "")
                {
                    model.Memberid = ds.Tables[0].Rows[0]["memberid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["xiangqing"] != null && ds.Tables[0].Rows[0]["xiangqing"].ToString() != "")
                {
                    model.Xiangqing = ds.Tables[0].Rows[0]["xiangqing"].ToString();
                }
                
                return model;
            }
            else
            {
                return null;
            }
        }





    }
}
